//
//  IntroduceView.m
//  Daybook
//
//  Created by dllo on 16/5/30.
//  Copyright © 2016年 zhangbin. All rights reserved.
//

#import "IntroduceView.h"
#import "IntroduceTableCell.h"
#import "Common.h"

@interface IntroduceView ()
<
UITableViewDataSource,
UITableViewDelegate
>
@property (nonatomic, strong) UIView *topView;
@property (nonatomic, strong) UIButton *backButton;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIView *lineView;

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) UIView *remarksView;
@property (nonatomic, strong) UILabel *remarksTitleLabel;
@property (nonatomic, strong) UILabel *remarksDetailLabel;

@property (nonatomic, strong) UIButton *editButton;
@property (nonatomic, strong) UIButton *deleteButton;
@end

@implementation IntroduceView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createView];
    }
    return self;
}
- (void)setMoney:(NSString *)money {
    if (_money != money) {
        _money = money;
        [_tableView reloadData];
    }
}- (void)setTime:(NSString *)time {
    if (_time != time) {
        _time = time;
        [_tableView reloadData];
    }
}
- (void)setCategory:(NSString *)category {
    if (_category != category) {
        _category = category;
        [_tableView reloadData];
    }
}
- (void)setType:(NSString *)type {
    if (_type != type) {
        _type = type;
        [_tableView reloadData];
    }
}
- (void)setRemarks:(NSString *)remarks {
    if (_remarks != remarks) {
        _remarks = remarks;
        if (_remarks == nil) {
            _remarksDetailLabel.text = @"未填写";
        }
        else {
            _remarksDetailLabel.text = _remarks;
            _remarksDetailLabel.numberOfLines = 0;
            [_remarksDetailLabel sizeToFit];
        }
        
        
    }
}
- (void)createView {
//    self.backgroundColor = [UIColor colorWithWhite:0.99 alpha:1];
    self.backgroundColor = LIGHT_GRAY_SECTION;
    
    self.topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 64/ 736.0 * self.frame.size.height)];
    _topView.backgroundColor = [UIColor whiteColor];
    [self addSubview:_topView];
    
    self.backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _backButton.frame = CGRectMake(10 / 414.0 * self.frame.size.width, 30 / 736.0 * self.frame.size.height, 24 / 414.0 * self.frame.size.width, 24 / 736.0 * self.frame.size.height);
    // _backButton.backgroundColor = [UIColor blueColor];
    [_topView addSubview:_backButton];
    [_backButton setImage:[UIImage imageNamed:@"fanhui.png"] forState:UIControlStateNormal];
    [_backButton addTarget:self action:@selector(backButton:) forControlEvents:UIControlEventTouchUpInside];
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 150 / 414.0 * self.frame.size.width, 44 / 736.0 * self.frame.size.height)];
    [_topView addSubview:_titleLabel];
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    _titleLabel.text = @"明细";
    _titleLabel.font = [UIFont systemFontOfSize:17 / 414.0 * self.frame.size.width];
    _titleLabel.center = CGPointMake(_topView.center.x, 42/ 736.0 * self.frame.size.height);
    self.lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 63 / 736.0 * self.frame.size.height, self.frame.size.width, 1)];
    _lineView.backgroundColor = LINE_COLOR;
    [self addSubview:_lineView];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 100 / 736.0 * self.frame.size.height, self.frame.size.width, self.frame.size.height / 3) style:UITableViewStylePlain];
    [self addSubview:_tableView];
    _tableView.rowHeight = _tableView.frame.size.height / 3;
    _tableView.delegate= self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_tableView registerClass:[IntroduceTableCell class] forCellReuseIdentifier:NSStringFromClass([IntroduceTableCell class])];
    _tableView.scrollEnabled = NO;
    
    self.remarksView = [[UIView alloc] init];
    [self addSubview:_remarksView];
    _remarksView.backgroundColor = [UIColor whiteColor];
    
    self.remarksTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake([CalculateFrameView setWidth:25], 0, self.frame.size.width / 3, self.frame.size.height / 8 / 2)];
    _remarksTitleLabel.text = @"备注";
    _remarksTitleLabel.textAlignment = NSTextAlignmentLeft;
    [_remarksView addSubview:_remarksTitleLabel];
    _remarksTitleLabel.font = [UIFont systemFontOfSize:15 / 414.0 * self.frame.size.width];
    //_remarksTitleLabel.backgroundColor = [UIColor redColor];
    
    self.remarksDetailLabel = [[UILabel alloc] initWithFrame:CGRectMake([CalculateFrameView setWidth:25], self.frame.size.height / 8 / 2, self.frame.size.width - 40 / 414.0 * self.frame.size.width, 40 / 736.0 * self.frame.size.height)];
    [_remarksView addSubview:_remarksDetailLabel];
//    _remarksDetailLabel.backgroundColor = [UIColor yellowColor];
    _remarksDetailLabel.textColor = [UIColor lightGrayColor];
    _remarksDetailLabel.font = [UIFont systemFontOfSize:15 / 414.0 * self.frame.size.width];
    if (_remarks == nil) {
        _remarksDetailLabel.text = @"未填写";
    }
    else {
        _remarksDetailLabel.text = _remarks;
    }
    
//    _remarksDetailLabel.numberOfLines = 0;
//    [_remarksDetailLabel sizeToFit];
    
    _remarksView.frame = CGRectMake(0, 130 / 736.0 * self.frame.size.height + self.frame.size.height / 3, self.frame.size.width, self.frame.size.height / 8 / 2 + _remarksDetailLabel.frame.size.height + 10 / 736.0 * self.frame.size.height);
    
    self.editButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _editButton.frame = CGRectMake([CalculateFrameView setWidth:25], self.frame.size.height / 3 + self.frame.size.height / 6 / 2 + _remarksDetailLabel.frame.size.height + 180 / 736.0 * self.frame.size.height, [CalculateFrameView setWidth:365], self.frame.size.height / 18);
    [self addSubview:_editButton];
    _editButton.backgroundColor = [UIColor whiteColor];
    [_editButton setTitle:@"编辑" forState:UIControlStateNormal];
    _editButton.titleLabel.font = [UIFont systemFontOfSize:17 / 414.0 * self.frame.size.width];
    _editButton.titleLabel.textAlignment = NSTextAlignmentLeft;
    [_editButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    _editButton.layer.borderWidth = 1;
    _editButton.layer.borderColor = [UIColor orangeColor].CGColor;
    [_editButton addTarget:self action:@selector(editButton:) forControlEvents:UIControlEventTouchUpInside];
    
    
    self.deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _deleteButton.frame = CGRectMake([CalculateFrameView setWidth:25], self.frame.size.height / 3 + self.frame.size.height / 8 / 2 + _remarksDetailLabel.frame.size.height + 210 / 736.0 * self.frame.size.height + self.frame.size.height / 11, [CalculateFrameView setWidth:365], self.frame.size.height / 18);
    [self addSubview:_deleteButton];
    _deleteButton.backgroundColor = [UIColor whiteColor];
    [_deleteButton setTitle:@"删除" forState:UIControlStateNormal];
    _deleteButton.titleLabel.font = [UIFont systemFontOfSize:17 / 414.0 * self.frame.size.width];
    _deleteButton.titleLabel.textAlignment = NSTextAlignmentLeft;

    [_deleteButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    _deleteButton.layer.borderWidth = 1;
    _deleteButton.layer.borderColor = [UIColor orangeColor].CGColor;
    [_deleteButton addTarget:self action:@selector(deleteButton:) forControlEvents:UIControlEventTouchUpInside];
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    IntroduceTableCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([IntroduceTableCell class])];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (0 == indexPath.row) {
        cell.titleLabel.text = @"记账金额";
        cell.detailLabel.text = _money;
    }
    else if (1 == indexPath.row) {
        cell.titleLabel.text = @"分类";
        cell.detailLabel.text = [NSString stringWithFormat:@"%@ > %@", _category, _type];
    }
    else {
        cell.titleLabel.text = @"记账时间";
        cell.detailLabel.text = _time;
    }
    return cell;
}
//返回
- (void)backButton:(UIButton *)button {
    [self.delegate popViewController];
}
//编辑
- (void)editButton:(UIButton *)button {
    [self.delegate editButtonAction];
}
//删除
- (void)deleteButton:(UIButton *)button {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"确认删除" message:nil delegate:self cancelButtonTitle:@"确认" otherButtonTitles:@"取消", nil];
    [alert show];
    
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 0) {
        [self.delegate deleteButtonAction];
    }
}


@end
